#ifndef NETWORKDELEGETE_H_20120412
#define NETWORKDELEGETE_H_20120412


////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 此文件包含网络回调信息。若需要处理网络返回的信息，需要继承此类，并在虚函数中处理消息
//
//
//                                             Wander 2012.04.12
/////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 连接账户服务器——————发送用户名密码——————发送连接区域———————连接游戏服务器——————创建新角色
//      |                  |                |                 |              
// 返回连接结果        验证，返回选区      返回服务器账密      返回角色信息
//                                             Wander 2012.04.12
/////////////////////////////////////////////////////////////////////////////////////////////////////
#include <string>
#include <vector>

#include "Network.h"

struct RegionInfo
{
	int  _id;			// 服务器ID
	int  _userCount;	    // 游戏人数
	char _szName[32];    // 服务器名称
};

struct RegionUser
{
    bool _bGetData;
    char _szName[32];
    char _serverAddr[32];
    unsigned int _psw;
};

struct GameInitPacket
{
    int  _bLeft;
    bool _bFirstRound;
    int  _minAngle;
    int  _maxAngle;
    
    int  _idLeft;
    char _szNameLeft[32];
    int  _hpLeft;
    int  _xPosLeft;
    int  _yPosLeft;
    
    int  _idRight;
    char _szNameRight[32];
    int  _hpRight;
    int  _xPosRight;
    int  _yPosRight;
    
    
};

struct FightPacket
{
    int   _id;
    float _xMove;
    float _yMove;
    
    float _power;
    float _angle;
    
    float _endTime;
    float _hp;
    
    int  _nextHeight;
};

struct RoleInfo
{
    int  _id;
    char _szName[32];
};

class CAccountDelegate
{
public:
    // 返回连接结果
	virtual void connectServerBack(bool bSuccess) = 0;
	// 返回登录结果
	virtual void userLoginBack(bool bSuccess, std::vector<RegionInfo > *pRegionList)  = 0;
	// 返回注册结果
	virtual void userRegisterBack(bool bSuccess)  = 0;
	// 返回选区结果
	virtual void chooseRegionBack(bool bSuccess, RegionUser *pUserInfo)  = 0;
    
	virtual void otherReceiveMsg(int id) = 0;
    
	virtual void otherAccountMsg(int id)  = 0;
    
    virtual void accountServerLost()  = 0;
};

class CFightDelegate
{
public:
    virtual void enemyMove(int id, const Move &moveInfo) = 0;
    virtual void attackSuccess(int id, const AttactRecv &attackInfo) = 0;
	virtual void roomChat(int id, const char *szRoom, int len) = 0;
};

class CGameDelegate
{
public:
    // 需要创建一个人物
    
    // 返回连接区域信息
    virtual void connectRegionBack(bool bSuccess)  = 0;
    // 返回登陆区域信息，如果成功，可以进行游戏了
    virtual void loginRegionBack(bool bSuccess)  = 0;
    virtual void startFightBack(bool bSuccess, const anFight &p)  = 0;  
    virtual void regionNoRole()    = 0;
    virtual void gameServerLost()  = 0;
    virtual void userLost()        = 0;
	virtual void createNameRepeat() = 0;
	virtual void recvChatFromName(const char *szName, const char *szMsg, int len) = 0;
};

class CServerHelper
{
protected:
    CServerHelper()
        : m_pAccountDelegate(NULL)
        , m_pGameDelegate(NULL)
        , m_pAccountServer(NULL)
        , m_pGameServer(NULL)
        , m_pFightDelegate(NULL)
    {
        m_regionUser._bGetData = false;
    }
    
    static CServerHelper *s_pServerHelper;
    
public:
    static CServerHelper *GetInstance();
    
    void SetAccountDelegate(CAccountDelegate *pDelegate){m_pAccountDelegate = pDelegate;}
    void SetGameDelegate(CGameDelegate *pDelegate){m_pGameDelegate = pDelegate;}
    void SetFightDelegate(CFightDelegate *pDelegate) {m_pFightDelegate = pDelegate;}
    
    void DisconnetAll();
	void ConnectServer();
	void UserLogin(const char *szName , const char *szPsw);
    
	void UserRegister(const char *szName , const char *szPsw, std::string strEmail);
	bool ChooseRegion(int id);
    bool ConnectRegion();
    
    bool LoginRegion();
    bool CreateRole(const RoleInfo &info);
    
    bool CreateRoom();
    //bool EnterRandomRoom();
    //bool LeaveRoom();
    
    //bool StartFight();
    
	// 继承类必须在适当的位置（比如Update函数）里调用此函数，从队列去消息
	void ReceivePacket();
    
    bool SendFightMove(const Move &moveInfo);
    bool SendFightAttack(const AttactSend &attackInfo);
	bool SendRoomChat(const char *szRoom, int len);
	bool SendChatToName(const char *szName, const char *szMsg, int msgLen);
    
protected:
    void _ReceiveAccountPacket();
    void _ReceiveGamePacket();
private:
    CAccountDelegate *m_pAccountDelegate;
    CGameDelegate    *m_pGameDelegate;
    CFightDelegate   *m_pFightDelegate;
    
    CAccountServer   *m_pAccountServer;
    CGameServer      *m_pGameServer;
    
    RegionUser        m_regionUser;
};


#endif